package com.witmore.skyline.web.controller;

import com.witmore.skyline.web.config.GlobalAccountSession;
import com.witmore.skyline.web.controller.vo.param.DriverAddParam;
import com.witmore.skyline.web.controller.vo.param.DriverDeleteParam;
import com.witmore.skyline.web.controller.vo.param.DriverListParamVO;
import com.witmore.skyline.web.controller.vo.result.*;
import com.witmore.skyline.web.service.DriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 驾驶员相关接口 V2.1
 *
 * @author 豆子高
 * @since 2025-10-27
 */
@RestController
@RequestMapping("api/driver/")
public class DriverController {

    @Autowired
    private DriverService driverService;

    /**
     * 驾驶员列表（分页） V2.1
     */
    @GetMapping(value = "driverListPage", name = "驾驶员列表（分页）")
    public ResponsePageResult<DriverResultVO> driverListPage(DriverListParamVO param) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        param.setOrgId(accountInfo.getOrgId());
        return driverService.driverListPage(param);
    }

    /**
     * 新增/编辑驾驶员 V2.1
     */
    @PostMapping(value = "saveOrUpdate", name = "新增/编辑驾驶员")
    public ResponseResult<Void> saveOrUpdate(@Valid @RequestBody DriverAddParam param) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        param.setOrgId(accountInfo.getOrgId());
        return driverService.saveOrUpdate(param);
    }

    /**
     * 删除驾驶员 V2.1
     */
    @PostMapping(value = "delete", name = "删除驾驶员")
    public ResponseResult<Void> delete(@Valid @RequestBody DriverDeleteParam param) {
        return driverService.delete(param);
    }

    /**
     * 驾驶员详情 V2.1
     */
    @GetMapping(value = "detail", name = "驾驶员详情")
    public ResponseResult<DriverResultVO> detail(@RequestParam("id") Integer id) {
        return driverService.getDetail(id);
    }

    /**
     * 获取可用驾驶员列表（未绑定设备的驾驶员，用于绑定设备时选择） V2.1
     */
    @GetMapping(value = "availableDriverList", name = "获取可用驾驶员列表")
    public ResponseResult<List<DriverSimpleVO>> availableDriverList() {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        return driverService.getAvailableDriverList(accountInfo.getOrgId());
    }
}
